package com.example.sxy.saxdemo;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;


public class MainActivity extends Activity {

    private List<Entity> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (list == null) {
            list = new ArrayList<Entity>();
        }

        parserXml();

        TextView tx = (TextView) findViewById(R.id.text_view);
        //输出解析出来的值
        StringBuilder text = new StringBuilder("******员工信息******\n\n");
         for (Entity each : list) {
             text.append("姓名：" + each.getName() + "\n\n" + "年龄：" + each.getAge() + "\n\n\n");
         }
        tx.setText(text);
    }

    private void parserXml() {
        //获取AssetManager管理器对象
        AssetManager as = this.getAssets();
        //通过AssetManager的open方法获取到beauties.xml文件的输入流
        try {
            //通过AssetManager的open方法获取到beauties.xml文件的输入流
            InputStream is = as.open("saxtest.xml");
            //通过获取到的InputStream来得到InputSource实例
            InputSource inputSource = new InputSource(is);

            //使用工厂方法初始化SAXParserFactory变量spf
            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
            //通过SAXParserFactory得到SAXParser的实例
            SAXParser saxParser = saxParserFactory.newSAXParser();
            //通过SAXParser得到XMLReader的实例
            XMLReader xmlReader = saxParser.getXMLReader();
            //初始化自定义的类MySaxHandler的变量msh，将beautyList传递给它，以便装载数据
            SaxHandler msh = new SaxHandler(list);
            //将对象msh传递给xr
            xmlReader.setContentHandler(msh);
            //调用xr的parse方法解析输入流
            xmlReader.parse(inputSource);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
